use std::path::Path;
use thiserror::Error;

#[cfg(feature = "security")]
use ring::{signature, rand};

#[derive(Error, Debug)]
pub enum SecurityError {
    #[error("Signature validation failed")]
    ValidationError,
    #[error("Sandbox error")]
    SandboxError,
}

pub struct ModelValidator {
    #[cfg(feature = "security")]
    public_key: signature::UnparsedPublicKey<Vec<u8>>,
}

impl ModelValidator {
    #[cfg(feature = "security")]
    pub fn new(pub_key: &[u8]) -> Self {
        Self {
            public_key: signature::UnparsedPublicKey::new(
                &signature::ED25519,
                pub_key.to_vec(),
            ),
        }
    }

    pub fn validate_model(
        &self,
        model_path: &Path,
        signature: &[u8],
    ) -> Result<(), SecurityError> {
        #[cfg(feature = "security")]
        {
            use std::fs;
            let model_data = fs::read(model_path)?;
            self.public_key.verify(&model_data, signature)
                .map_err(|_| SecurityError::ValidationError)
        }
        #[cfg(not(feature = "security"))]
        Ok(())
    }
}